<template>
  <div>
    <van-list :error.sync="error" error-text="请求失败，点击重新加载" v-model="loading" :finished="finished" finished-text="没有更多了"
      @load="onLoad">
      <!-- <van-cell v-for="item in list" :key="item.com_id+''" :title="item.content" /> -->
      <CommentItem @click-reply="$emit('click-reply',$event)" @updatacount="$event ? item.like_count++:item.like_count--" v-model="item.is_liking" :item="item"
        v-for="item of list" :key="item.com_id+''" />
    </van-list>
  </div>
</template>

<script>
import { getcommentList } from '@/api/comment/index.js'
import CommentItem from '@/components/comment-item/index.vue'
// 该组件会进行复用
// 组件在不同地方用传不同的type
export default {
  name: 'comment',
  props: {
    // 区分获取评论的数据类型
    type: {
      type: String,
      default: 'a'
    },
    source: {
      type: [Object, Number],
      require: true
    },
    // 父向子
    list: {
      type: Array,
      required: true
    }

  },
  data () {
    return {

      loading: false,
      finished: false,
      limit: 10, // 每页条数
      offset: null,
      total_count: 0,
      error: false
    }
  },
  methods: {
    // dada(e) {
    //   if(e) {
    //     this.
    //   }
    // },
    async onLoad () {
      // 1、发送请求获取数据
      try {
        // if (Math.random() > 0.5) {
        //   console.lg(11)
        // }
        const { data: res } = await getcommentList({
          type: this.type,
          source: this.source + '',
          offset: this.offset,
          limit: this.limit
        })
        console.log(res)
        // 2、保存数据
        this.list.push(...res.data.results)
        this.total_count = res.data.total_count
        // 子向父传评论总条数
        this.$emit('updataTotal', this.total_count)
        // 3、加载状态结束
        this.loading = false
        // 4、数据全部加载完成
        if (this.list.length === this.total_count) {
          this.finished = true
        } else {
          this.offset = res.data.last_id
        }
      } catch (err) {
        // this.$toast('获取评论失败')
        this.loading = false

        this.error = true
      }

      // this.loading = false

      // this.finished = true
    }
  },
  computed: {},
  watch: {},
  filters: {},
  components: {
    CommentItem
  },
  created () {
    // console.log(this.source)
  },
  mounted () { }
}
</script>

<style scoped lang='less'>
</style>
